<?php

namespace App\Http\Controllers;

use Throwable;
use Exception;
use App\Models\Genre;
use Illuminate\Support\Facades\Auth;
use App\Http\Requests\Logic\Genre\QueryResource;
use App\Http\Requests\Logic\Genre\StoreResource;
use App\Http\Requests\Logic\Genre\UpdateResource;
use App\Http\Requests\Logic\Genre\QueryCollection;
use App\Http\Requests\Logic\Genre\DestroyResource;

/**
 * 排班类型逻辑控制器
 *
 * Date: 22/04/2018
 * @author George
 * @package App\Http\Controllers
 */
class GenreController extends Controller
{
	/**
	 * 获取资源集合
	 *
	 * Date: 21/04/2018
	 * @author George
	 * @param QueryCollection $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function index(QueryCollection $request)
	{
		$project_id = $request->get('project_id', Auth::user()->project_id);
		$query = Genre::query();
		$genres = $query->where('project_id', $project_id)->get();
		return success($genres);
	}

	/**
	 * 根据ID获取指定资源
	 *
	 * Date: 21/04/2018
	 * @author George
	 * @param QueryResource $request
	 * @param Genre $genre
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function show(QueryResource $request, Genre $genre)
	{
		return success($genre);
	}

	/**
	 * 创建排班类型
	 *
	 * Date: 21/04/2018
	 * @author George
	 * @param StoreResource $request
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Throwable
	 */
	public function store(StoreResource $request)
	{
		$user = Auth::user();
		$attributes = $this->extractInputFromRules($request, $request->rules());
		try {
			$genre = Genre::create($attributes);
			return stored($genre);
		} catch (Throwable $exception) {
			return internalError('创建失败');
		}
	}

	/**
	 * 更新排班类型
	 *
	 * Date: 21/04/2018
	 * @author George
	 * @param UpdateResource $request
	 * @param Genre $genre
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function update(UpdateResource $request, Genre $genre)
	{
		$attributes = $this->extractInputFromRules($request, $request->rules());
		try {
			$genre->update($attributes);
			return updated($genre);
		} catch (Exception $exception) {
			return internalError('更新失败');
		}
	}

	/**
	 * 删除排班类型
	 *
	 * Date: 21/04/2018
	 * @author George
	 * @param DestroyResource $reques
	 * @param Genre $genre
	 * @return \Illuminate\Http\JsonResponse
	 * @throws Exception
	 */
	public function destroy(DestroyResource $reques, Genre $genre)
	{
		$genre->delete();
		return deleted();
	}
}
